﻿Imports System.Windows.Forms
Imports ArfaUI
Public Class MasterProduksiPage
    Inherits pagetemplate
    Dim ProcName As String
    Dim Params() As Object
    Private Function GetData() As DataTable
        Dim dt As New DataTable
        If Utils.executeSP(ProcName, Params, dt) Then

        End If
        Return dt
    End Function
    Private Sub SetData()
        Dim dt As New DataTable
        If Utils.executeSP(ProcName, Params, dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    MsgBox(dt(0)(0))
                End If
            End If
        End If
    End Sub
    Private Function validasiRow(ByVal dtv As DataTable) As Boolean
        Dim retval As Boolean = False
        If dtv IsNot Nothing Then
            If dtv.Rows.Count > 0 Then
                retval = True
            End If
        End If
        Return retval
    End Function
    Private Function GetAllEqual(ByVal tblname As String, ByVal finder As String, ByVal finderkey As String) As DataTable
        Dim dt As New DataTable
        Params = New Object() {"*", tblname, finder, finderkey}
        If Utils.executeSP("proc_searchallEqual", Params, dt) Then

        End If
        Return dt
    End Function

    Private Sub createtree()
        Dim dtroot As New DataTable
        Dim dtchlid As New DataTable
        ProcName = "proc_produkgroup"
        Params = New Object() {"select", Convert.DBNull, Convert.DBNull}
        dtroot = GetData()
        If validasiRow(dtroot) Then
            For Each drg As DataRow In dtroot.Rows
                Dim node As New TreeNode
                node.Text = drg("namagroupproduk")
                node.Tag = drg("groupproduk")
                node.Name = "G" & drg("groupproduk")
                TreeMenu.Nodes.Add(node)
                dtchlid = New DataTable
                dtchlid = GetAllEqual("vproduk", "groupproduk", drg("groupproduk"))
                If validasiRow(dtchlid) Then
                    Dim nodes() As TreeNode
                    nodes = TreeMenu.Nodes.Find("G" & drg("groupproduk"), True)
                    If nodes.Count() = 1 Then
                        For i As Integer = 0 To dtchlid.Rows.Count - 1
                            Dim inode As New TreeNode
                            inode.Text = dtchlid(i)("namaproduk")
                            inode.Tag = dtchlid(i)("idproduk")
                            inode.Name = dtchlid(i)("idproduk")
                            nodes(0).Nodes.Add(inode)
                        Next
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub MasterProduksiPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub

    Private Sub LinkExpand_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkExpand.LinkClicked
        TreeMenu.ExpandAll()
    End Sub

    Private Sub LinkCollapse_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkCollapse.LinkClicked
        TreeMenu.CollapseAll()
    End Sub

    Private Sub TreeMenu_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeMenu.AfterSelect
        DGVDetail.DataSource = Nothing
        txtProduksi.Text = ""
        txtKode.Text = ""
        txtNama.Text = ""
        If TreeMenu.SelectedNode Is Nothing Then
            Exit Sub
        End If
        If TreeMenu.SelectedNode.Name.StartsWith("G") Then
            Dim dt As New DataTable
            txtProduksi.Text = TreeMenu.SelectedNode.Text
            ProcName = "proc_searchallEqual"
            Params = New Object() {"idproduk, namaproduk, groupproduk, namagroupproduk, qty, countitemproduk(idproduk) as jumlahitem", "vproduk", "groupproduk", TreeMenu.SelectedNode.Tag}
            dt = GetData()
            If validasiRow(dt) Then
                DGVDetail.DataSource = dt
                DGVDetail.Columns(2).Visible = False
                'DGVDetail.Columns(1).Visible = False
                'DGVDetail.Columns(2).Visible = False
            End If
        Else
            Dim dt As New DataTable
            txtProduksi.Text = TreeMenu.SelectedNode.Parent.Text
            txtNama.Text = TreeMenu.SelectedNode.Text
            txtKode.Text = TreeMenu.SelectedNode.Tag
            ProcName = "proc_searchallEqual"
            Params = New Object() {"idproduk, namaproduk, qty, iditem, namaitem, qtyitem, satuan", "vprodukdetail", "idproduk", TreeMenu.SelectedNode.Tag}
            dt = GetData()
            If validasiRow(dt) Then

                dt.Columns.RemoveAt(0)
                dt.Columns.RemoveAt(0)
                dt.Columns.RemoveAt(0)

                DGVDetail.DataSource = dt
            End If
        End If
        'TreeGrid1.ProcName = New String() {"proc_searchallEqual", "proc_searchallEqual"}
        'Dim obj() As Object
        'Dim param As New List(Of Object)
        'param.Clear()
        'obj = New Object() {"*", "rsproduksi", "idproduksi", TreeMenu.SelectedNode.Tag}
        'param.Add(obj)
        'obj = New Object() {"idproduksi,iditem,namaitem,qtyitem,satuanjual as satuan", "vproduksidetail", "idproduksi", TreeMenu.SelectedNode.Tag}
        'param.Add(obj)

        'TreeGrid1.Params = param.ToArray
        'TreeGrid1.displaycolumn.Clear()
        ''TreeGrid1.displaycolumn.Add()
        'TreeGrid1.displaycolumn.Add("idproduksi")
        'TreeGrid1.displaycolumn.Add("namaproduk")
        'TreeGrid1.displaycolumn.Add("namaitem")
        'TreeGrid1.displaycolumn.Add("qtyitem")
        'TreeGrid1.displaycolumn.Add("satuan")

        'TreeGrid1.setGridPproperty("idproduksi")

    End Sub

    Private Sub MasterProduksiPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged

        TreeMenu.Nodes.Clear()
        createtree()
        txtProduksi.Text = ""
        'TreeGrid1.TreeGridView.DataSource = Nothing
    End Sub

    Private Sub MbGlassButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton4.Click
        Dim DialogGroupProduk As New DialogGroupProduk
        DialogGroupProduk.ShowDialog()
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub

    Private Sub MbGlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton1.Click
        Dim DialogProduk As New DialogProduk
        DialogProduk.ShowDialog()
        TreeMenu.Nodes.Clear()
        createtree()
    End Sub

    Private Sub MbGlassButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton2.Click
        If txtKode.Text = String.Empty Then
            MsgBox("ID / Nama Produk Tidak Valid")
        End If
        Dim DialogItemProduk As New DialogItemProduk
        DialogItemProduk.vidproduk.Text = txtKode.Text
        DialogItemProduk.vnamaproduk.Text = txtNama.Text
        DialogItemProduk.ShowDialog()
        Dim dt As New DataTable
        
        ProcName = "proc_searchallEqual"
        Params = New Object() {"idproduk, namaproduk, qty, iditem, namaitem, qtyitem, satuan", "vprodukdetail", "idproduk", txtKode.Text}
        dt = GetData()
        If validasiRow(dt) Then

            dt.Columns.RemoveAt(0)
            dt.Columns.RemoveAt(0)
            dt.Columns.RemoveAt(0)

            DGVDetail.DataSource = dt
        End If
    End Sub
End Class
